package crivia.eca.am2.page;

import java.util.ArrayList;
import java.util.List;

import crivia.db.common.SQL;
import crivia.db.exception.NoData;
import crivia.db.i.DataSet;
import crivia.eca.ECA;
import crivia.ecp.ECP;
import crivia.ecp.carry.html.X;
import crivia.ecp.carry.html.div.Div;
import crivia.ecp.carry.html.form.Form;
import crivia.ecp.carry.html.form.Input;
import crivia.ecp.carry.html.form.Select;
import crivia.ecp.carry.html.table.Table;
import crivia.ecp.carry.html.table.TableRow;

public class RequestDataLooker extends ECP {

	public static String PageKey = "RequestDataLooker";
	@Override
	public String pageKey() {
		return PageKey;
	}
	
	public enum K {
		x
		,cdMixBy
		,workflowID
		,requestID
		,requestMark
		,requestName
	}
	
	@Override
	protected String HTMLBody() {
		StringBuilder html = new StringBuilder();
		{
			Table t = new Table();
			t.setBorderColor1px("#000");
			t.addRow(X.cs("Search By ...", "#000", true))
				.addStyle("text-align", "left");
			kvf(t,"Condition Mix By",new Select(this, K.cdMixBy, null)
					.addOption("or").addOption("and"));
			kvf(t,"Request ID",new Input(this, K.requestID));
			kvf(t,"Request Mark",new Input(this, K.requestMark));
			kvf(t,"Request Name",new Input(this, K.requestName));
			kvf(t,"Workflow ID",new Input(this, K.workflowID));
			t.addRow(new X("input",null).addAttr("type", "submit")
				.addAttr("value", "Execute Search."));
			html.append(t);
		}
		if (rpExist(K.x)) {
			int requestID = -1;
			int workflowID = -1;
			{
				Table t = new Table();
				t.setBorderColor1px("#000");
				t.addRow(X.cs("Table Information ...", "#000", true))
					.addStyle("text-align", "left");
				{
					String cd;
					if ("or".equals(rp(K.cdMixBy))){
						cd = SQL.cd.or(
							SQL.cd.equals("requestID", rp(K.requestID), true)
							,SQL.cd.equals("requestName", rp(K.requestName), true)
							,SQL.cd.equals("requestMark", rp(K.requestMark), true)
							,SQL.cd.equals("workflowID", rp(K.workflowID), true)
						);
					} else {
						cd = SQL.cd.and(
							SQL.cd.equals("requestID", rp(K.requestID), true)
							,SQL.cd.equals("requestName", rp(K.requestName), true)
							,SQL.cd.equals("requestMark", rp(K.requestMark), true)
							,SQL.cd.equals("workflowID", rp(K.workflowID), true)
						);
					}
					DataSet d = SQL.select(SQL.sql.array("select"
						+" workflowID w,requestID r"
						+" from workflow_requestBase"
						+" where "+cd+" order by requestID desc"
					),defaultCMD);
					t.addRow(SQL.LastSQL);
					if (d.next()){
						requestID = d.getInt("r", 0);
						workflowID = d.getInt("w", 0);
						kvf(t,"Request ID" , requestID);
						kvf(t,"Workflow ID" , workflowID);	
						d = SQL.select(SQL.sql.array("select"
							+" formid f from workflow_base"
							+" where id = ?" , workflowID
						),defaultCMD);
						t.addRow(SQL.LastSQL);
						kvf(t,"Form ID" , d.get("f"));
					} else {
						t.addRow("No Data.");
					}
				}
				html.append(t);
			}
			if (requestID > 0 && workflowID > 0){
				Table t = new Table();
				t.setBorderColor1px("#000");
				t.addRow(X.cs("Request Data ...", "#000", true))
					.addStyle("text-align", "left");
				int k = 0;
				int mainID = -1;
				while (true){
					try {
						String tableName = ECA.getRequestTableName(""+workflowID,k);
						if (k == 0){
							kvf(t,"Main Table",X.cs(tableName, "#000", true));
							DataSet d = SQL.select(SQL.sql.array("select *"
								+" from "+tableName
								+" where requestID = ?"
							, requestID ) , defaultCMD);
							t.addRow(SQL.LastSQL);
							mainID = d.get("id",int.class);
							kvf(t,"ID (MainID in Detail Table)", mainID);
							for (String f : d.keys()){
								if ("id".equals(f)) continue;
								kvf(t,f, d.get(f));
							}
						} else if (mainID > 0) {
							kvf(t,"Detail Table "+k,X.cs(tableName, "#000", true));
							DataSet d = SQL.select(SQL.sql.array("select *"
								+" from "+tableName
								+" where mainID = ?"
							, mainID ) , defaultCMD);
							t.addRow(SQL.LastSQL);
							Table s = new Table();
							t.addRow(s).getCell(0)
								.addStyle("padding", "2px");
							s.setBorderColor1px("#000");
							TableRow sh = s.addRow(X.cs("ID", "#000", true));
							List<String> fs = new ArrayList<String>();
							for (String f : d.keys()){
								if ("id".equals(f)||"mainid".equals(f)) continue;
								fs.add(f);
								sh.addCell(X.cs(f, "#000", true));
							}
							while (d.next()){
								TableRow r = s.addRow(d.get("id"));
								for (String f : fs){
									r.addCell(d.get(f));
								}
							}
						}
					} catch (NoData e) { break; }
					k ++;
				}
				html.append(t);
			}
		}
		Form f = new Form(this, formKey, EU(this));
		f.setInner(html.toString());
		f.addHidden(""+K.x , "1");
		return ""+new Div().setInner(f)
				.addStyle("text-align", "center");
	}
	private void kvf(Table t,String k,Object v){
		TableRow r = t.addRow();
		r.addCell(k).addStyle("text-align", "right")
			.addStyle("padding-right", "17px");
		r.addCell(v).addStyle("text-align", "left")
			.addStyle("padding-left", "17px");
	}

}
